package com.xie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xie.domain.Result;

import com.xie.pojo.Appartus;
import com.xie.pojo.Potion;
import com.xie.service.AppartusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "仪器管理")
@RestController
@RequestMapping(value = "/Appartus")
public class AppartusController {
    @Autowired
    private AppartusService appartusService;

    /**
     * 分页
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @ApiOperation("获取分页仪器数据")
    @GetMapping("{pageIndex}/{pageSize}")
    public Result findAppartus(@PathVariable Integer pageIndex
            , @PathVariable Integer pageSize){
        Page page=new Page(pageIndex,pageSize);
        IPage page1 = appartusService.page(page);
        if (page1!= null) return Result.ok(page1);
        return Result.fail();
    }

    /**
     * 模糊查询
     * @param params
     * @return
     */
    @GetMapping("find/{params}")
    @ApiOperation("模糊查询仪器信息")
    public Result FindPage(@PathVariable String params){
        LambdaQueryWrapper<Appartus> lambdaQueryWrapper=new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Appartus::getAppartusId,params)
                .or().like(Appartus::getAppartusName,params)
                .or().like(Appartus::getAppartusNo,params)
                .or().like(Appartus::getAppartusPrice,params)
                .or().like(Appartus::getAppartusQuantity,params)
                .or().like(Appartus::getAppartusRen,params)
        ;
        List<Appartus> list = appartusService.list(lambdaQueryWrapper);
        if (list != null) return Result.ok(list);
        return Result.fail();
    }

    /**
     * 添加
     * @param appartus
     * @return
     */
    @ApiOperation("添加仪器")
    @PostMapping("/save")
    public Result addAppartus(@RequestBody Appartus appartus) {
        boolean save = appartusService.save(appartus);
        if (!save) {
            return Result.fail();
        }
        return Result.ok();
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation("根据id删除仪器")
    @DeleteMapping("/removeById/{id}")
    public Result deleteAdmin(@PathVariable Integer id) {
        boolean b = appartusService.removeById(id);
        if (!b) {
            return Result.fail();
        }
        return Result.ok();
    }

    /**
     * 修改
     * @param appartus
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改仪器数据")
    public Result updateAdmin(@RequestBody Appartus appartus) {
        boolean update = appartusService.updateById(appartus);
        if (!update) return Result.fail();
        return Result.ok();
    }


    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("findById/{id}")
    @ApiOperation("根据id获取仪器数据")
    public Result findById(@PathVariable Long id) {
        Appartus ap2= appartusService.getById(id);
         if (ap2 != null) {
             return Result.ok(ap2);
         }
         return Result.fail();
    }


    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取全部仪器数据")
    public Result exportAdmin(){
        List<Appartus> appartus = appartusService.list();
        if (appartus!= null) return Result.ok(appartus);
        return Result.fail();
    }
}
